<?php
/**
 * Product:     One-Page Checkout Booster
 * Package:     Gold_Gonepage_1.1.1_94708
 * Purchase ID: 9CBm2i1g9YwZZ6QK4IjUneSwxlnJsImwioL1MUWAXd
 * Generated:   2011-07-15 10:07:59
 * File path:   app/code/local/Gold/Gonepage/Block/Checkout.php
 * Copyright:   (c) 2011 GOLD, Inc.
 */
class Gold_Gonepage_Block_Checkout extends Mage_Checkout_Block_Onepage_Abstract
{ 
	
	public function getSteps()
    {
        $steps = array();

        if (!$this->isCustomerLoggedIn()) {
            $steps['login'] = $this->getCheckout()->getStepData('login');
            Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::getUrl('*/*', array('_secure'=>true)));
        }

        $stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'review');

        foreach ($stepCodes as $step) {
            $steps[$step] = $this->getCheckout()->getStepData($step);
        }
        return $steps;
    }
    
	public function getActiveStep()
    {
       return array('login');
    }
    
    protected function _beforeToHtml()
    {
        parent::_beforeToHtml();
        foreach(Mage::helper('gonepage/Gconfcheckout')->getDisabledSectionHash() as $disabledStep)
        {
            $this->unsetChild($disabledStep);
        }
    }
    
    protected function _afterToHtml($html)
    {
        if($this->helper('gonepage')->isShowCheckoutInCart() && $this->helper('gonepage')->isCompactDesign()) {
            $content = $this->getLayout()->getBlock('content');
            $content->setText( preg_replace('|\<div class="totals"\>(.*)\</div\>|Uis','', $content->getText()) );
        }
        return parent::_afterToHtml($html);
    }    
    
    protected function _setCompactTemplate() {
        $this->getLayout()->getBlock('head')->addItem('skin_css', 'css/gold/gonepage/compact.css'); 
        $this->setTemplate($this->getCompactTemplate());
    }
    
    public function setContext($context) 
    {           
        $cartBlock = $this->getLayout()->getBlock('checkout.cart');   
        
        if (!$this->helper('gonepage')->isShowCheckoutInCart() || $this->helper('gonepage')->isDisabled())
        {
            $cartBlock->getParentBlock()->unsetChild('gonepage.checkout');
        } else {
            $this->getCheckout()->setCartWasUpdated(false);
            $cartBlock->unsetChild('shipping');    
            
            //if($this->helper('gonepage')->isCompactDesign()) {
                $cartBlock->unsetChild('crosssell');
                $cartBlock->unsetChild('coupon');
                $cartBlock->unsetChild('totals');
                $cartBlock->unsetChild('top_methods');
                $cartBlock->unsetChild('methods');
               // $cartBlock->getChild('top_methods')->unsetChild('checkout.cart.methods.onepage');
           // }
            if (!$this->helper('gonepage')->isShowProceedToCheckoutButton()) 
            {
                //$cartBlock->getChild('top_methods')->unsetChild('checkout.cart.methods.onepage');
                //$cartBlock->getChild('methods')->unsetChild('checkout.cart.methods.onepage');
            }
            if ($this->getQuote()->hasItems() && !$this->getQuote()->validateMinimumAmount())
            {
                if ($this->helper('gonepage')->isShowCheckoutInCart())
                {
                    $error = Mage::getStoreConfig('sales/minimum_order/error_message');
                    $this->getCheckout()->addError($error);
                }
            }   
        }                                       
    }
    
    public function chooseTemplate()
    {
        if ($this->getQuote()->getItemsCount()) {
            if($this->helper('gonepage')->isCompactDesign()) {
                $this->_setCompactTemplate();
            } else {
                $this->setTemplate($this->getCheckoutTemplate());
            }
        } else {
            $this->setTemplate($this->getEmptyTemplate());
        }
    }
    
    public function getContinueShoppingUrl()
    {
        $url = $this->getData('continue_shopping_url');
        if (is_null($url)) {
            $url = Mage::getSingleton('checkout/session')->getContinueShoppingUrl(true);
            if (!$url) {
                $url = Mage::getUrl();
            }
            $this->setData('continue_shopping_url', $url);
        }
        return $url;
    }
    
    /**
     * Return list of available checkout methods
     *
     * @param string $nameInLayout Container block alias in layout
     * @return array
     */
    public function getMethods($nameInLayout)
    {
        if ($this->getChild($nameInLayout) instanceof Mage_Core_Block_Abstract) {
            return $this->getChild($nameInLayout)->getSortedChildren();
        }
        return array();
    }
    
    /**
     * Return HTML of checkout method (link, button etc.)
     *
     * @param string $name Block name in layout
     * @return string
     */
    public function getMethodHtml($name)
    {
        $block = $this->getLayout()->getBlock($name);
        if (!$block) {
            Mage::throwException(Mage::helper('checkout')->__('Invalid method: %s', $name));
        }
        return $block->toHtml();
    }
} 